<?php

namespace app\admin\controller\customer;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 服务内容
 *
 * @icon fa fa-circle-o
 */
class Deal extends Backend
{
    protected $dataLimit = 'auth'; //默认基类中为false，表示不启用，可额外使用auth和personal两个值
    protected $dataLimitField = 'admin_id';
    /**
     * Deal模型对象
     * @var \app\admin\model\customer\Deal
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\customer\Intention();

    }

    public function approved($ids)
    {
        $customer = $this->model::get($ids);
        if (!$customer) {
            $this->error();
        }
        $customer->save(['status' => 2, 'customer_type' => 1]);
        $this->success('操作成功');
    }

    public function failed($ids)
    {
        $customer = $this->model::get($ids);
        if (!$customer) {
            $this->error();
        }
        $customer->save(['status' => 3]);
        $this->success('操作成功');
    }

    public function cjxx($ids)
    {
        $customer = $this->model::get($ids);
        if (!$customer) {
            return false;
        }
        if (false === $this->request->isPost()) {
            return $this->view->fetch('', ['row' => $customer]);
        }
    }

    public function fpkj($ids)
    {
        $customer = $this->model::get($ids);
        if (!$customer) {
            return false;
        }

        if (false === $this->request->isPost()) {
            $admin = Db::name('admin')->where(['identity' => 1])->select();

            $admin_list = [];
            foreach ($admin as $item) {
                $admin_list[$item['id']] = $item['nickname'];
            }

            $customer->account_ids = explode(',', $customer->account_ids);
            return $this->view->fetch('', ['admin' => $admin_list, 'row' => $customer]);
        }

        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $params = $this->preExcludeFields($params);

        $result = false;
        Db::startTrans();
        try {
            if (!empty($params['account_ids'])) {
                $params['account_ids'] = implode(',', $params['account_ids']);
            }

            $result = $customer->allowField(true)->save($params);

            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success();
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
